package com.ojoin.trade.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TimeUtils {

	private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.S", "yyyy-MM-dd HH:mm", "yyyy-MM",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
			"yyyy.MM.dd HH:mm", "yyyy.MM" };

	private static Logger log = LoggerFactory.getLogger(TimeUtils.class);

	/**
	 * DateUtils类 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss",
	 * "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss",
	 * "yyyy/MM/dd HH:mm", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
	 * "yyyy.MM.dd HH:mm" }
	 */
	public static Date parseDate(Object str) {
		if (str == null) {
			return null;
		}
		try {
			return DateUtils.parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	public static String toZhTimeString(long l) {
		long day = l / (24 * 60 * 60 * 1000);
		long hour = (l / (60 * 60 * 1000) - day * 24);
		long minute = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long second = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60);
		StringBuilder sb = new StringBuilder();
		if (day > 0) {
			sb.append(day).append("天,");
		}
		if (hour > 0) {
			sb.append(hour).append("时");
		}
		if (minute > 0) {
			sb.append(minute).append("分");
		}
		if (second > 0) {
			sb.append(second).append("秒");
		}
		return sb.toString();
	}

	public static String getShortCurTime() {
		return new SimpleDateFormat("HHmmss").format(new Date());
	}

	public static String getShortCurDate() {
		return new SimpleDateFormat("yyyyMMdd").format(new Date());
	}
	
	public static String getShortYesterdayDate() {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, -1);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		return formatter.format(date);
	}
	

	public static String getCurDateInFile() {
		return new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
	}

	public static String getCurDateTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}

	public static String getCurDateString() {
		return new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
	}
	
	public static String getCurDateStringNoSplit() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}

	public static String getDateString(Date date) {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}

	public static String getCurYear() {
		return new SimpleDateFormat("yyyy").format(new Date());
	}

	public static String getCurTime() {
		return new SimpleDateFormat("HH:mm:ss").format(new Date());
	}

	public static String getCurDate() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}
	public static String getCurDate(Date date) {
		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}
	

	public static Date getDate(String date) {
		return getDate("yyyy-MM-dd", date);
	}

	public static Date getDate(String format, String date) {
		Date myDate = new Date();

		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			myDate = sdf.parse(date);
		} catch (ParseException e) {
			LogUtils.info(log, e.getMessage());
		}

		return myDate;
	}

	public static String getYesterdayDateTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date myDate = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(myDate);
		c.add(Calendar.DAY_OF_MONTH, -1);
		myDate = c.getTime();
		return formatter.format(myDate);
	}

	public static String getYesterdayDate() {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, -1);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(date);
	}

	public static String getCurDay() {
		return new SimpleDateFormat("dd").format(new Date());
	}

	public static String getCurDay(String date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date cdt = null;
		try {
			cdt = df.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new SimpleDateFormat("dd").format(cdt);
	}

	public static String getTimeSlash(String time) {
		String rtime = "";
		rtime = time.replaceAll("-0", "-").replaceAll("-", "/");
		return rtime;

	}

	/**
	 * 获取过去的分钟
	 * 
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / (60 * 1000);
	}

	/**
	 * 计算两个日期的差几天
	 * currDate-date
	 * @param date
	 * @return 
	 * @throws Exception
	 */
	public static long getDateSpaceOfDay(String date) throws Exception {
		return getDateSpaceOfDay(date, getCurDate());
	}

		
	/**
	 * 计算两个日期的差几天
	 * date2-date1
	 * @param date1
	 * @param date2
	 * @return 
	 * @throws Exception
	 */
	public static long getDateSpaceOfDay(String date1, String date2) throws Exception {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat df2 = new SimpleDateFormat("yyyy/MM/dd");
		long to;
		long from;
		if (date1.contains("-")) {
			to = df.parse(date1).getTime();
		} else {
			to = df2.parse(date1).getTime();
		}
		if (date2.contains("-")) {
			from = df.parse(date2).getTime();
		} else {
			from = df2.parse(date2).getTime();
		}

		return (from - to) / (1000 * 60 * 60 * 24);
	}

	/**
	 * 计算两个日期的差几天
	 * date2-date1
	 * @param date1
	 * @param date2
	 * @return
	 * @throws Exception
	 */
	public static long getDateSpaceOfDay(Date date1, Date date2) throws Exception {

		long to = date1.getTime();
		long from = date2.getTime();
		
		return (from - to) / (1000 * 60 * 60 * 24);
	}
	
	// 计算与时间date相差days天数的时间
	public static String getDateAfterDays(String date, int days) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date cdt = df.parse(date);

		DateTime cdtt = new DateTime(cdt);

		DateTime oneMonthAfter = cdtt.plusDays(days);

		String end = oneMonthAfter.toString("yyyy-MM-dd");
		return end;
	}

	// 计算与时间date相差days天数的时间
	public static String getDateBefore(String date, int days) throws ParseException {
		return getDateAfterDays(date, -days);
	}

	public static String getDateAfterDays(int days) {
		DateTime cdt = new DateTime();
		DateTime oneMonthAfter = cdt.plusDays(days);
		String end = oneMonthAfter.toString("yyyy-MM-dd");
		return end;
	}

	public static String getDateBefore(int days) {
		return getDateAfterDays(-days);
	}

	public static Date nowUTC() {
		return new DateTime(DateTimeZone.UTC).toDate();
	}
	
	/**
	 * 
	 * @return
	 */
	public static String getLastBefore(int[] clearingsDay,String time){
	
		int day = Integer.valueOf(TimeUtils.getCurDay(time));
		String strday;
		String needtime;
		
		if (clearingsDay[0] <= day && day < clearingsDay[1]) {
			strday = String.valueOf(clearingsDay[0]);
		} else if (clearingsDay[1] <= day && day < clearingsDay[2]) {
			strday = String.valueOf(clearingsDay[1]);
		} else {
			strday =String.valueOf(clearingsDay[2]);
		}
		
		if(Integer.valueOf(strday) < 10){
			strday = "0"+strday;
		}
		needtime = time.substring(0, 8) + strday;
		
		
		return needtime;
		
	}

	/**
	 * 
	 * @return
	 */
	public static String getLastBefore(int[] clearingsDay){
		String date = TimeUtils.getCurDate();
		
		return getLastBefore(clearingsDay,date);
		
		
	}
	
	public static String getTimeByFullDate(String fulldate) {
		return new SimpleDateFormat("HH:mm:ss").format(parseDate(fulldate));
	}

	public static String getDateByFullDate(String fulldate) {
		return new SimpleDateFormat("yyyy-MM-dd").format(parseDate(fulldate));
	}
	
	public static void main(String[] args) {
//		String dateByFullDate = TimeUtils.getDateByFullDate("2016-06-05 12:12:12.123");
//		System.out.println(dateByFullDate);
		
		String dateBefore = TimeUtils.getDateBefore(2);
		System.out.println(dateBefore);
	}
}
